﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;
using System.Configuration;
using System.Data;
using System.Text;
using System.Net;

namespace MyVocationSpace
{
    public partial class Header : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            BAL.conn.connectionString = ConfigurationManager.ConnectionStrings["connectionstring"].ConnectionString;
            if (Session["UserID"] != null)
            {
                if (Session["IsAdmin"] != null)
                {
                    if ((bool)Session["IsAdmin"])
                    {
                        accountDiv.Style.Add("visibility", "visible");
                        lnkLogin.Visible = true;
                        imgIndicator.Visible = true;
                        lnkLogout.Visible = true;
                        lnkChngPass.Visible = true;
                        liMyDashBoard.Visible = true;
                        logincontainer.Visible = false;
                        peoplesearch.Visible = true;
                        liMyvocation.Visible = true;
                        liCreateTopic.Visible = true;
                        lnkApproveIndustry.Visible = true;
                        lnkApproveVocation.Visible = true;
                    }
                    else
                    {
                        lnkApproveVocation.Visible = false;
                    }
                }

                //else
                //{
                accountDiv.Style.Add("visibility", "visible");
                lnkLogin.Visible = true;
                imgIndicator.Visible = true;
                lnkLogout.Visible = true;
                lnkChngPass.Visible = true;
                liMyDashBoard.Visible = true;
                //lnkFofgetPswd.Visible = true;
                logincontainer.Visible = false;
                peoplesearch.Visible = true;
                // liInboxe.Visible = true;
                liMyvocation.Visible = true;
                //liProfile.Visible = true;
                liCreateTopic.Visible = true;
                //}

                // lnkJoin.Visible = false;
                //  ScriptManager.RegisterClientScriptBlock(Page, this.GetType(), "ActiveLink", "ActiveTopLink();", true);
                if (Session["UserName"] != null)
                {
                    lnkLogin.Text = Session["UserName"].ToString();
                }
            }

            if (Session["UserID"] == null)
            {
                if ((Request.QueryString["log"] == "1") || (Request.QueryString["WhatIsMVS"] == "2"))
                {
                    logincontainer.Visible = false;
                    peoplesearch.Visible = false;
                }
            }

            if (!IsPostBack)
            {
                //lnkLogin.Attributes.Add("onmouseover", "tooltip.show('My Profile');");
                //lnkLogin.Attributes.Add("onmouseout", "tooltip.hide();");          
            }

            IsIndustrySelected();
        }

        public void IsIndustrySelected()
        {
            DataSet ds = BAL.Users.GetVocationByUser(Convert.ToInt32(Session["UserID"]));
            if (ds.Tables.Count > 0)
            {
                if (ds.Tables[0].Rows.Count > 0)
                {
                    liMyDashBoard.Visible = true;
                    liCreateTopic.Visible = true;
                    liMyvocation.Visible = true;
                    // Response.Redirect("MyDashboard.aspx");
                }
                else
                {
                    liMyDashBoard.Visible = false;
                    liCreateTopic.Visible = false;
                    liMyvocation.Visible = false;
                    //Response.Redirect("AddMyVocation.aspx");
                }
            }       
        }

        protected void btnLogin_Click(object sender, EventArgs e)
        {
            string strEmail = txtEmail.Text;
            string strPassword = BAL.Encryption.Encript(txtPass.Text, "&%#@?,:*");
            string UserID = BAL.Users.VerifyUserID(strEmail, strPassword);

            if (UserID == "-1" || UserID == "-2")
            {

                ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "ErrorPopup", "ErrorPopup('Sorry!! Email ID or Password you entered is incorrect');", true);
            }
            else if (UserID == "0")
            {
                ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "ErrorPopup", "ErrorPopup('Your Email ID is not verified yet. <br> Click on Confirmation link sent to your Email ID to verify.');", true);
                // "Your Email ID is not verified yet. <br> Click on Confirmation link sent to your Email ID to verify.";
            }
            else
            {
                //Session.Add("UserID", strVal);

                Session.Add("UserID", UserID);
                Session.Add("UserEmail", strEmail);
                string[] userDetails = BAL.Users.GetUserDetails(Convert.ToInt32(UserID));
                string Name = userDetails[1] + " " + userDetails[2];
                lnkLogin.Text = Name;

                Session.Add("UserName", Name);

                //string isAdmin = userDetails[3];
                //Session.Add("IsAdmin", isAdmin);

                string Country = userDetails[15];
                Session.Add("IDCountry", Country);


                bool admiflag = BAL.Users.GetAdminFlag(Convert.ToInt32(UserID));
                Session.Add("IsAdmin", admiflag);

                string IDIndustry = userDetails[17];
                Session.Add("IDIndustry", IDIndustry);

                DataSet ds = BAL.Users.GetVocationByUser(Convert.ToInt32(Session["UserID"]));
                if (ds.Tables.Count > 0)
                {
                    if (ds.Tables[0].Rows.Count > 0)                  //If has vocation
                    {
                        Response.Redirect("MyDashboard.aspx");
                    }
                    else
                    {
                        Response.Redirect("AddMyVocation.aspx");
                    }
                }
            }
        }
        protected void SignOut(object sender, EventArgs e)
        {
            FormsAuthentication.SignOut();

            Session.RemoveAll();
            Session.Abandon();
            Session.Clear();
            logincontainer.Visible = true;
            peoplesearch.Visible = false;
            //liProfile.Visible = true;
            liMyvocation.Visible = true;
            liCreateTopic.Visible = true;
            //liInboxe.Visible = true;

            //lnkJoin.Visible = true ;
            lnkLogin.Visible = false;
            imgIndicator.Visible = false;
            lnkMyVocations.Visible = false;
            Response.Redirect("Home.aspx");

        }

        protected void imgSearchBtn_Click(object sender, ImageClickEventArgs e)
        {
            string StrSearch = txtSearchtext.Text;
            if (StrSearch == "Search...")
            {
                StrSearch = "";
            }

            Response.Redirect("SearchPeople.aspx?strSearch=" + StrSearch + "");

        }
    }
}